<?php

namespace app\shop\controller;

use think\Controller;

class Back extends Controller
{
    protected $BusinessModel = null;
    protected $OrderModel = null;
    protected $OrderProductModel = null;
    protected $RecordModel = null;
    protected $ProductModel = null;
    protected $AddressModel = null;
    protected $ExpressqueryModel = null;
    protected $BackModel = null;
    protected $BackProductModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
        $this->OrderModel = model('Order.Order');
        $this->OrderProductModel = model('Order.Product');
        $this->RecordModel = model('Business.Record');
        $this->ProductModel = model('Product.Product');
        $this->AddressModel = model('Business.Address');
        $this->ExpressqueryModel = model('Expressquery');
        $this->BackModel = model('Depot.Back.Back');
        $this->BackProductModel = model('Depot.Back.Product');
    }


    //添加退货信息
    public function add()
    {
        if ($this->request->isAjax()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $orderid = $this->request->param('orderid', 0, 'trim');
            $expressid = $this->request->param('expressid', 0, 'trim');
            $expresscode = $this->request->param('expresscode', '', 'trim');
            $addrid = $this->request->param('addrid', 0, 'trim');
            $remark = $this->request->param('remark', '', 'trim');

            $business = $this->BusinessModel->find($busid);

            if (!$business) {
                $this->error('用户不存在');
                exit;
            }

            $order = $this->OrderModel->find($orderid);

            if (!$order) {
                $this->error('订单不存在');
                exit;
            }

            //订单商品
            $product = $this->OrderProductModel->where(['orderid' => $orderid])->select();

            if (!$product) {
                $this->error('订单里面没有商品');
                exit;
            }

            $address = $this->AddressModel->find($addrid);

            if (!$address) {
                $this->error('收获地址不存在');
                exit;
            }

            // 生成退货订单号
            $code = build_code('BK');

            $BackData = [
                'code' => $code,
                'ordercode' => $order['code'],
                'busid' => $busid,
                'contact' => $address['consignee'],
                'province' => $address['province'],
                'city' => $address['city'],
                'district' => $address['district'],
                'address' => $address['address'],
                'phone' => $address['mobile'],
                'amount' => $order['amount'],
                'expressid' => $expressid,
                'expresscode' => $expresscode,
                'remark' => $remark,
                'status' => '0', //未审核
                'adminid' => '1', //超级管理员
            ];

            //判断是否有上传图片
            if (isset($_FILES['thumbs'])) {
                $success = build_uploads('thumbs');

                if ($success['code']) {
                    $BackData['avatar'] = implode(',', $success['data']);
                } else {
                    $this->error($success['msg']);
                    exit;
                }
            }

            $this->BackModel->startTrans();
            $this->BackProductModel->startTrans();

            $BackStatus = $this->BackModel->validate('common/Depot/Back/Back')->save($BackData);

            if ($BackStatus === FALSE) {
                $this->error($this->BackModel->getError());
                exit;
            }

            //组装退货的商品
            $BackProduct = [];
            foreach ($product as $item) {
                $BackProduct[] = [
                    'backid' => $this->BackModel->id,
                    'proid' => $item['proid'],
                    'nums' => $item['pronum'],
                    'price' => $item['price'],
                    'total' => $item['total'],
                ];
            }

            $BackProductStatus = $this->BackProductModel->validate('common/Depot/Back/Product')->saveAll($BackProduct);

            if ($BackProductStatus === FALSE) {
                $this->BackModel->rollback();
                $this->error($this->BackProductModel->getError());
                exit;
            }

            if ($BackStatus === FALSE || $BackProductStatus === FALSE) {
                $this->BackProductModel->rollback();
                $this->BackModel->rollback();
                $this->error('填写退货单失败');
                exit;
            } else {
                $this->BackModel->commit();
                $this->BackProductModel->commit();
                $this->success('填写退货单成功');
                exit;
            }
        }
    }
}
